package com.leaf.spring.y201907.day30.kafka;

import java.util.Collections;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

/**
 * 消费者
 *@Title:  
 *@Description:  
 *@Author: leaf  
 *@Since:2019年7月30日  
 *@Version:1.1.0
 */
public class TestConsumer {

	public static void main(String[] args) {
		Properties properties = new Properties();
		properties.setProperty("bootstrap.servers", "192.168.89.40:9092");
		properties.setProperty("key.deserializer", StringDeserializer.class.getName());
		properties.setProperty("value.deserializer", StringDeserializer.class.getName());
		properties.setProperty("group.id", "testGroup");

		KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);
		consumer.subscribe(Collections.singletonList("test-topic"));

		while (true) {
			ConsumerRecords<String, String> records = consumer.poll(500);
			for (ConsumerRecord<String, String> consumerRecord : records) {
				System.out.println("消息所在分区："+consumerRecord.partition()+",偏移量："+consumerRecord.offset()+",key="+consumerRecord.key()+",value="+consumerRecord.value());
			}
		}
	}

}
